import "package:ct_pa_majordomo/pages/apartment/index.dart";
import "package:ct_pa_majordomo/pages/home/index.dart";
import "package:ct_pa_majordomo/pages/mine/index.dart";
import "package:ct_pa_majordomo/pages/residents/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Main extends StatefulWidget {
  final int? index;
  final Map? params;
  const Main({super.key, this.index, this.params});

  @override
  State<Main> createState() => _MainState();
}

class _MainState extends State<Main> {
  int _current = 0;
  
  // 为了保持页面状态，不使用UniqueKey
  final GlobalKey<HomeState> _homeKey = GlobalKey<HomeState>();
  final GlobalKey<ApartmentsState> _apartmentsKey = GlobalKey<ApartmentsState>();
  final GlobalKey<ResidentsLayoutState> _residentsKey = GlobalKey<ResidentsLayoutState>();
  final GlobalKey<MineState> _mineKey = GlobalKey<MineState>();

  @override
  void initState() {
    super.initState();
    _current = widget.index ?? 0;
  }

  // 刷新当前页面
  void _refreshCurrentPage() {
    switch (_current) {
      case 0:
        if (_homeKey.currentState != null) {
          _homeKey.currentState!.refresh();
        }
        break;
      case 1:
        if (_apartmentsKey.currentState != null) {
          _apartmentsKey.currentState!.refresh();
        }
        break;
      case 2:
        if (_residentsKey.currentState != null) {
          _residentsKey.currentState!.refresh();
        }
        break;
      case 3:
        if (_mineKey.currentState != null) {
          _mineKey.currentState!.refresh();
        }
        break;
    }
  }

  BottomNavigationBarItem _renderItem(IconData icon, String label) =>
      BottomNavigationBarItem(icon: Icon(icon, size: 28), label: label);

  @override
  Widget build(BuildContext context) {
    Widget current = Scaffold(
      body: IndexedStack(
        index: _current,
        children: [
          Home(key: _homeKey),
          Apartments(key: _apartmentsKey, params: widget.params),
          ResidentsLayout(key: _residentsKey),
          Mine(key: _mineKey)
        ],
      ),
      
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(splashColor: Colors.transparent),
        child: BottomNavigationBar(
          backgroundColor: Colors.white,
          currentIndex: _current,
          selectedFontSize: 12,
          type: BottomNavigationBarType.fixed,
          onTap: (value) {
            // 如果点击的是当前页面，调用刷新方法
            if (_current == value) {
              _refreshCurrentPage();
            } else {
              // 切换页面
              setState(() => _current = value);
              
              // 延迟200毫秒后刷新新页面，确保页面已经加载完成
              Future.delayed(const Duration(milliseconds: 200), () {
                _refreshCurrentPage();
              });
            }
          },
          items: [
            _renderItem(Icons.home_filled, "首页"),
            _renderItem(Icons.apartment_rounded, "房源"),
            _renderItem(Icons.person_pin_rounded, "入住人"),
            _renderItem(Icons.account_box_rounded, "我的"),
          ],
        ),
      ),
    );
    if (_current != 2) {
      current = AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child: current,
      );
    }
    return current;
  }
}
